package com.jss.lanouhn.fristapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.Interface.OnItemClickLister;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.adapters.CategoryActivityAdapter;
import com.jss.lanouhn.fristapp.modles.CategoryActivityData;

import java.io.IOException;

import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CategoryStrategyActivity extends AppCompatActivity {
    private CategoryActivityData data;
    private CategoryActivityAdapter adapter;
    private String url = "http://api.liwushuo.com/v2/columns?limit=20&offset=0";
    private ImageView btnBackCredit2;
    private RecyclerView rvCategoryActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_strategy);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        btnBackCredit2= (ImageView) findViewById(R.id.btn_back_credit2);
        btnBackCredit2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        rvCategoryActivity= (RecyclerView) findViewById(R.id.rv_category_activity);
        rvCategoryActivity.setLayoutManager(new LinearLayoutManager(CategoryStrategyActivity.this));
    }

    private void initData() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client=new OkHttpClient();
                Request request=new Request.Builder()
                        .url(url)
                        .build();
                Call call=client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result=response.body().string();
                        Gson gson=new Gson();
                        data=gson.fromJson(result,CategoryActivityData.class);
                        if(data.getData().getColumns().size()>0)
                            handler.sendEmptyMessage(1);
                    }
                });
            }
        }).start();
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
          if (msg.what==1){
              adapter=new CategoryActivityAdapter(CategoryStrategyActivity.this, data);
              adapter.setOnItemClickLister(new OnItemClickLister() {
                  @Override
                  public void onItemClick(View view, int position) {
                      Intent intent = new Intent(CategoryStrategyActivity.this, HomeTitleActivity.class);
                      intent.putExtra("id", data.getData().getColumns().get(position).getId() + "");
                      startActivity(intent);
                  }
              });
              rvCategoryActivity.setAdapter(adapter);
          }
        }
    };
}
